package com.huawei.servlet;

import com.huawei.bean.User;
import com.huawei.dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用于修改数据时使用
 * 修改数据后对数据库进行修改操作
 */
@WebServlet(value = "/update.do")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码格式 防止乱码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        int id =0;
        String idStr = req.getParameter("id");
        if (idStr!=null && idStr!=""){
            id = Integer.parseInt(idStr);
        }
        UserDao userDao = new UserDao();
        User user =UserDao.findUserById(id);
        if (user!=null){
            req.setAttribute("User",user);
            req.getRequestDispatcher("/show.jsp").forward(req,resp);
        }else {
            resp.getWriter().println("该用户不存杂");
        }
    }
}
